//
//  CLLVisitorView.swift
//  CLWeiNo
//
//  Created by DDC on 2017/10/11.
//  Copyright © 2017年 DDC. All rights reserved.
//

import UIKit

class CLLVisitorView: UIView {

    override init(frame: CGRect) {
        super.init(frame: frame)
        setUpUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    /// 访客视图字典信息
    var visitorInfo:[String:String]?{
        didSet{
            guard let tipString = visitorInfo?["tipString"],
                  let imageName = visitorInfo?["imageName"]
            else {
                return
            }
            if imageName == "" {
                // 首页不需要加载图片
                startAnimation()
                return
            }
            // 重新赋值
            tipLabel.text = tipString
            backIcon.image = UIImage.init(named: imageName)
            
            houseIcon.isHidden = true
            maskIconView.isHidden = true
            
        }
    }
    /// 懒加载私有控件
    /// 圆圈背景
    fileprivate lazy var backIcon = UIImageView(image: UIImage(named: "visitordiscover_feed_image_smallicon"))
    /// 半透明遮罩
    fileprivate lazy var maskIconView = UIImageView(image: UIImage(named: "visitordiscover_feed_mask_smallicon"))
    /// 小房子
    fileprivate lazy var houseIcon = UIImageView(image: UIImage(named: "visitordiscover_feed_image_house"))
    /// 提示文字
    fileprivate lazy var tipLabel = UILabel.hm_label(withText: "关注一些人,回头看看这里有没有什么惊喜,关注一些人,回头看看这里有没有什么惊喜", fontSize: 14, textColor: UIColor.darkGray, alignment: .center)
    /// 注册按钮
     lazy var registerButton = UIButton.hm_button(withTitle: "注册", fontSize: 14, textColor: UIColor.darkGray, higlightTextColor: UIColor.orange, backgroudImage: "common_button_white_disable")
    /// 登录按钮
     lazy var logonButton = UIButton.hm_button(withTitle: "登录", fontSize: 14, textColor: UIColor.darkGray, higlightTextColor: UIColor.orange, backgroudImage: "common_button_white_disable")
    
}
extension CLLVisitorView {
    func setUpUI(){
        backgroundColor = UIColor.hm_color(withHex: 0xECECEC)
        // 添加私有控件
        addSubview(backIcon)
        addSubview(maskIconView)
        addSubview(houseIcon)
        addSubview(tipLabel)
        addSubview(registerButton)
        addSubview(logonButton)
        
        // 取消自动布局
        for v in subviews {
            v.translatesAutoresizingMaskIntoConstraints = false
        }
        // 自动布局控件
        // >背景
        addConstraint(NSLayoutConstraint(item: backIcon, attribute: .centerX, relatedBy: .equal, toItem: self, attribute: .centerX, multiplier: 1.0, constant: 0))
        addConstraint(NSLayoutConstraint(item: backIcon, attribute:.centerY, relatedBy: .equal, toItem: self, attribute: .centerY, multiplier: 1.0, constant: -60))
        // >小房子
        addConstraint(NSLayoutConstraint(item: houseIcon, attribute: .centerX, relatedBy: .equal, toItem: backIcon, attribute: .centerX, multiplier: 1.0, constant: 0))
        addConstraint(NSLayoutConstraint(item: houseIcon, attribute: .centerY, relatedBy: .equal, toItem: backIcon, attribute: .centerY, multiplier: 1.0, constant: 0))
        // >提示文字
        addConstraint(NSLayoutConstraint(item: tipLabel, attribute: .centerX, relatedBy: .equal, toItem: houseIcon, attribute: .centerX, multiplier: 1.0, constant: 0))
        addConstraint(NSLayoutConstraint(item: tipLabel, attribute: .top, relatedBy: .equal, toItem: backIcon, attribute: .bottom, multiplier: 1.0, constant: 20))
        addConstraint(NSLayoutConstraint(item: tipLabel, attribute: .width, relatedBy: .equal, toItem: nil, attribute: .notAnAttribute, multiplier: 1.0, constant: UIScreen.hm_screenWidth() - 120))
        // >注册按钮
        addConstraint(NSLayoutConstraint(item: registerButton, attribute: .left, relatedBy: .equal, toItem: tipLabel, attribute: .left, multiplier: 1.0, constant: 0))
        addConstraint(NSLayoutConstraint(item: registerButton, attribute: .top, relatedBy: .equal, toItem: tipLabel, attribute: .bottom, multiplier: 1.0, constant: 20))
        addConstraint(NSLayoutConstraint(item: registerButton, attribute: .width, relatedBy: .equal, toItem: nil, attribute: .notAnAttribute, multiplier: 1.0, constant: UIScreen.hm_screenWidth() / 3))
        // >登录按钮
        addConstraint(NSLayoutConstraint(item: logonButton, attribute: .right, relatedBy: .equal, toItem: tipLabel, attribute: .right, multiplier: 1.0, constant: 0))
        addConstraint(NSLayoutConstraint(item: logonButton, attribute: .top, relatedBy: .equal, toItem: tipLabel, attribute: .bottom, multiplier: 1.0, constant: 20))
        addConstraint(NSLayoutConstraint(item: logonButton, attribute: .width, relatedBy: .equal, toItem: nil, attribute: .notAnAttribute, multiplier: 1.0, constant: UIScreen.hm_screenWidth() / 3))
        // >半透明遮罩
        addConstraint(NSLayoutConstraint(item: maskIconView, attribute: .centerX, relatedBy: .equal, toItem: backIcon, attribute: .centerX, multiplier: 1.0, constant: 0))
        addConstraint(NSLayoutConstraint(item: maskIconView, attribute: .centerY, relatedBy: .equal, toItem: backIcon, attribute: .centerY, multiplier: 1.0, constant: 0))
    }
    
    /// 背景图片旋转的动画
    func startAnimation() {
     
        let anim = CABasicAnimation(keyPath:"transform.rotation")
        // 设置动画属性
        anim.toValue = Double.pi * 2
        anim.duration = 10
        anim.repeatCount = MAXFLOAT
        anim.isRemovedOnCompletion = false
        
        backIcon.layer.add(anim, forKey: nil)
        
    }
}

